package com.example.administrator.hqq.activity.home;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.example.administrator.hqq.R;
import com.example.administrator.hqq.adapter.group.TransferGroupListAdapter;
import com.example.administrator.hqq.base.MyActivity;
import com.example.administrator.hqq.bean.group.GetGroupSettingsBean;
import com.example.administrator.hqq.config.LoongsConfig;
import com.example.administrator.hqq.config.UrlFactory;
import com.example.administrator.hqq.utils.HintWordsUtils;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

public class TransferGroupActivity extends MyActivity {
    @BindView(R.id.return_auto)
    AutoRelativeLayout returnAuto;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.report_auto)
    AutoRelativeLayout reportAuto;
    @BindView(R.id.list)
    ListView list;
    @BindView(R.id.hint)
    TextView hint;
    @BindView(R.id.cancel)
    TextView cancel;
    @BindView(R.id.confirm)
    TextView confirm;
    @BindView(R.id.hint_auto)
    AutoLinearLayout hintAuto;
    private List<GetGroupSettingsBean.DataBean.MemberBean> lists;
    private String id;
    private String TAG = this.getClass().getSimpleName();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_transfer_group);
        ButterKnife.bind(this);
        initview();
    }

    private void initview() {
        hintAuto.setVisibility(View.GONE);
        lists = (List<GetGroupSettingsBean.DataBean.MemberBean>) getIntent().getSerializableExtra(LoongsConfig.member);
        TransferGroupListAdapter transferGroupListAdapter = new TransferGroupListAdapter(this, lists);
        list.setAdapter(transferGroupListAdapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                hint.setText("确定选择 " + lists.get(i).getName() + "为新群主,你将自动放弃群主身份.");
                hintAuto.setVisibility(View.VISIBLE);
                id = lists.get(i).getId();
            }
        });
    }

    @OnClick({R.id.return_auto, R.id.cancel, R.id.confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.return_auto:
                finish();
                break;
            case R.id.cancel:
                finish();
                break;
            case R.id.confirm:
                OkHttpUtils.get().url(UrlFactory.setNewOwner())
                        .addParams(LoongsConfig.group_id, getIntent().getStringExtra(LoongsConfig.group_id))
                        .addParams(LoongsConfig.old_owner_id, LoongsConfig.getUser_id())
                        .addParams(LoongsConfig.new_owner_id, id)
                        .build().execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e(TAG, "onError: " + e);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e(TAG, "onResponse: " + response);
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            HintWordsUtils.showMarkeWords(jsonObject.getInt(LoongsConfig.code), jsonObject.getString(LoongsConfig.description));
                            if (jsonObject.getInt(LoongsConfig.code) == 200) {
                                ToastUtils.showShort("转让成功");
                                setResult(110,getIntent());
                                finish();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
                break;
        }
    }

    @Override
    protected String getActivityName() {
        return this.getClass().getSimpleName();
    }
}
